package com.ufaud.ais.sys.webapp.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ufaud.ais.pub.webapp.action.BaseAction;
import com.ufaud.ais.sys.entity.CustomItem;
import com.ufaud.ais.sys.entity.CustomItemType;
import com.ufaud.ais.sys.service.ICustomItemService;

public class CustomItemAction extends BaseAction {

	protected static Log LOG = LogFactory.getLog(CustomItemAction.class);

	private Long typeId;
	private Long itemId;
	private CustomItem customItem;
	private CustomItemType customItemType;
	private List<CustomItem> customItemList;
	private List<CustomItemType> customItemTypeList;
	
	private ICustomItemService customItemService;
	
	public String toMain() {
		return SUCCESS;
	}
	
	public String listItem() {
		customItemType = customItemService.getTypeById(typeId);
		customItemList = customItemService.getItemListByTypeId(typeId);
		return SUCCESS;
	}
	
	public String listType() {
		customItemTypeList = customItemService.getAllType();
		return SUCCESS;
	}
	
	public String createType() {
		return SUCCESS;
	}
	
	public String createItem() {
		return SUCCESS;
	}
	
	public String editItem() {
		customItem = customItemService.get(itemId);
		return SUCCESS;
	}
	
	public String editType() {
		customItemType = customItemService.getTypeById(typeId);
		return SUCCESS;
	}
	
	public String doInsertType() {
		try {
			customItemService.insertType(customItemType);
			addJsonObjectProperty("success", "success");
		} catch(Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}
	
	public String doInsertItem() {
		try {
			customItemService.save(customItem);
			addJsonObjectProperty("success", "success");
		} catch(Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}
	
	public String doUpdateType() {
		try {
			customItemService.updateType(customItemType);
			addJsonObjectProperty("success", "success");
		} catch(Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}
	
	public String doUpdateItem() {
		try {
			customItemService.updateItem(customItem);
			addJsonObjectProperty("success", "success");
		} catch(Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}
	
	public String doDeleteType() {
		LOG.debug("typeId=" + typeId);
		try {
			customItemService.deleteTypeById(typeId);
			addJsonObjectProperty("success", "success");
		} catch(Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}
	
	public String doDeleteItem() {
		LOG.debug("itemId=" + itemId);
		try {
			customItemService.deleteItemById(itemId);
			addJsonObjectProperty("success", "success");
		} catch(Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public CustomItem getCustomItem() {
		return customItem;
	}

	public void setCustomItem(CustomItem customItem) {
		this.customItem = customItem;
	}

	public CustomItemType getCustomItemType() {
		return customItemType;
	}

	public void setCustomItemType(CustomItemType customItemType) {
		this.customItemType = customItemType;
	}

	public List<CustomItem> getCustomItemList() {
		return customItemList;
	}

	public void setCustomItemList(List<CustomItem> customItemList) {
		this.customItemList = customItemList;
	}

	public List<CustomItemType> getCustomItemTypeList() {
		return customItemTypeList;
	}

	public void setCustomItemTypeList(List<CustomItemType> customItemTypeList) {
		this.customItemTypeList = customItemTypeList;
	}

	public ICustomItemService getCustomItemService() {
		return customItemService;
	}

	public void setCustomItemService(ICustomItemService customItemService) {
		this.customItemService = customItemService;
	}
}
